/**
 * 信息Api
 * 获取一些公共信息
 */
const db = require('../../config/db')

/**
 * 获取学院列表
 * @param {*} req 
 * @param {*} res 
 */
exports.getColleges = async (req, res) => {
  try {
    const {collegeName} = req.body
    console.log("collegeName: ", collegeName) 
    let sql
    let params = []
    if(collegeName && collegeName.trim() != "") {
      sql= `SELECT id, college FROM 
            college where college LIKE ?`
      params.push(`%${collegeName}%`)
    } else {
      sql = 'SELECT id, college FROM college'
    }
    const [results] = await db.query(sql, params);
    console.log("params: ", params) 
    console.log("results: ", results) 
    res.json(results);
  } catch (err) {
    console.error("getColleges 报错了:", err);
    res.status(500).json({ error: '数据库查询失败' });
  }
};

/**
 * 获取运动列表
 */
exports.getSports = async (req, res) => {
    try {
        const {sportName} = req.body
        console.log("sportName: ", sportName) 
        let sql
        let params = []
        if(sportName && sportName.trim() != "") {
          sql= `SELECT id, sport FROM sport 
                where sport LIKE ?`
          params.push(`%${sportName}%`)
        } else {
          sql = 'SELECT id, sport FROM sport'
        }
        const [results] = await db.query(sql, params)
        res.json(results)
    } catch(err) {
        console.error("getSports报错了:", err)
        res.status(500).json({ error: '数据库查询失败' })
    }
}

/**
 * 提交反馈/建议
 * @param {*} req 
 * @param {*} res 
 */
exports.putFeedback = async(req, res) => {
  try {
    const openId = req.openId
    const {content} = req.body
    const sql = `INSERT INTO feedback (open_id, content, create_time) VALUES (?, ?, NOW())`;
    const [result] = await db.query(sql, [openId, content]);

    res.status(200).json({ message: '反馈提交成功', feedbackId: result.insertId });
  } catch(err) {
    console.error("putFeedback error: ", err)
    res.status(500).json({ error: "putFeedback error!" })
  }
}


